package com.briup.server.config;

import com.briup.common.constant.SystemConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮箱支持类，用于发送邮件
 */
@Component
@RequiredArgsConstructor
public class EmailSupporter {
    private final EMailProperties eMailProperties;
    private final JavaMailSender javaMailSender;

    /**
     * 发送简单邮件
     *
     * @param to      接收人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendEmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(eMailProperties.getNickname()
                        + " <" + eMailProperties.getUsername() + ">");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        javaMailSender.send(message);
    }

    /**
     * 发送简单邮件
     *
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param to      接收人邮箱地址，多个邮箱地址之间用逗号隔开
     */
    public void sendEmail(String subject, String content, String... to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(eMailProperties.getNickname()
                        + " <" + eMailProperties.getUsername() + ">");
        // 设置接收人邮箱地址，多个邮箱地址之间用逗号隔开
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        javaMailSender.send(message);
    }

    /**
     * 发送验证码邮件
     *
     * @param to   接收人邮箱地址
     * @param code 验证码
     */
    public void sendVerifyCode(String to, String code) {
        String content = String.format("您的验证码为：%s，有效时间为%d分钟", code, SystemConstant.CAPTCHA_EXPIRE_MINUTES);
        sendEmail(to, "登录验证码", content);
    }

    /**
     * 发送预约审批邮件，一旦由用户发起预约请求，就会发送审批邮件到对应管理员邮箱
     *
     * @param to      接收人邮箱地址，多个邮箱地址之间用逗号隔开
     * @param content 邮件内容
     */
    public void sendApprovalEmail(String content, String... to) {
        String subject = "实验室预约需要审批";
        sendEmail(subject, content, to);
    }
}
